<template>
  <!-- 会员管理 -->
  <div class="user_list">
    <div class="btns">
      <el-form :inline="true" size="small">
        <el-button type="primary" size="small" @click="toAdd">添加</el-button>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-form-item>
          <el-input
            placeholder="请输入会员名"
            v-model="param.username"
            clearable
          ></el-input>
        </el-form-item>
        <el-button size="small" icon="el-icon-search" @click="loadUsers"
          >搜索</el-button
        >
      </el-form>
    </div>
    <el-table :data="users.list" size="small" v-loading="loading">
      <el-table-column
        type="index"
        :index="1"
        label="序号"
        align="center"
        fixed="left"
      />
      <el-table-column prop="username" label="会员名" align="center" />
      <el-table-column prop="realname" label="姓名" align="center" />
      <el-table-column prop="gender" label="性别" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.gender === 'male'">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="telephone" label="手机号" align="center" />

      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="scope">
          <a
            type="text"
            style="color: #008964"
            size="small"
            @click.prevent="toEdit(scope.row)"
            >修改</a
          >
          <a
            type="text"
            style="color: #008964"
            size="small"
            @click.prevent="toDetails(scope.row)"
            >详情</a
          >
          <a
            type="text"
            style="color: #008964"
            size="small"
            @click.prevent="deleteHandler(scope.row.id)"
            >移除</a
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      small
      :hide-on-single-page="true"
      :total="users.total"
      :page-size="users.pageSize"
      :current-page="users.page"
      @current-change="currentChangeHandler"
    >
    </el-pagination>
    <!-- 分页 -->
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible">
      <el-form ref="user_form" :model="form" :rules="rules">
        <el-form-item label="会员名" label-width="80px" prop="username">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" label-width="80px" prop="realname">
          <el-input v-model="form.realname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" label-width="80px" prop="password">
          <el-input v-model="form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性别" label-width="80px" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="male">男</el-radio>
            <el-radio label="female">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" label-width="80px">
          <el-input v-model="form.telephone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="出生日期" label-width="80px">
          <el-date-picker
            v-model="form.birth"
            value-format="timestamp"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="地址" label-width="80px">
          <el-cascader
            v-model="params.value"
            clearable
            :options="pca"
            :props="{ children: 'children', label: 'name', value: 'name' }"
            @change="handleChange"
          ></el-cascader>
          <el-input
            v-model="params.address"
            style="width: 200px"
            placeholder="请输入详细地址"
            autocomplete="off"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="saveUserHandler"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</template>
<script>
import { get, post, del } from "@/utils/request";
import pca from "@/assets/pca.json";
import qs from "querystring";
export default {
  data() {
    return {
      pca,
      form: {
        roleId: 6,
      },
      loading: false,
      visible: false,
      title: "添加会员",
      user: {},
      users: [],
      roles: [],
      params: {},
      param: {
        page: 1,
        pageSize: 13,
        rolename: "会员",
      },
      props: { multiple: true, value: "id", label: "name", emitPath: false },
      rules: {
        username: [
          { required: true, message: "请输入会员名", trigger: "change" },
        ],
        realname: [
          { required: true, message: "请输入姓名", trigger: "change" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
        ],
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
      },
    };
  },
  created() {
    this.loadUsers();
  },
  methods: {
    saveUserHandler() {
      this.$refs["user_form"].validate((valid) => {
        if (valid) {
          let address = this.params.value.join("-") + "/" + this.params.address;
          const url = "/baseUser/addUserWidthRole";
          post(url, { ...this.form, address }).then((response) => {
            this.$message({ message: response.message, type: "success" });
            this.loadUsers();
            this.visible = false;
          });
        } else {
          return false;
        }
      });
    },
    toAdd() {
      this.title = "添加会员";
      this.form = {
        roleId: 6,
      };
      this.visible = true;
    },
    loadUsers() {
      this.loading = true;
      get("/baseUser/pageQuery", this.param).then((response) => {
        this.users = response.data;
        this.loading = false;
      });
    },
    toDetails(row) {
      // 跳转会员详情页面
      this.$router.push({
        path: "/members/InfoDetails",
        query: row,
      });
    },
    deleteHandler(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const url = "/baseUser/deleteById";
        get(url, { id }).then((response) => {
          this.$message({ type: "success", message: response.message });
          this.loadUsers();
        });
      });
    },
    toEdit(record) {
      let arr = record.address.split("/");
      this.params = Object.assign({}, this.params, { address: arr[1] });
      this.params = Object.assign({}, this.params, {});
      this.title = "修改会员信息";
      this.form = {
        ...record,
        roleId: 6,
      };
      this.visible = true;
    },
    // 当前页发生改变
    currentChangeHandler(page) {
      this.param.page = page;
      this.loadUsers();
    },
    handleChange(value) {
      // console.log(value);
      this.params.value = value;
    },
  },
};
</script>
